package it.blog.controller;

import it.blog.stub.*;

import java.io.File;
import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.xml.ws.wsaddressing.W3CEndpointReference;


public class RemoveObject extends HttpServlet{
	
	
	
	/**
	 * 
	 */
	private static final long serialVersionUID = -6769028325821570155L;

	@Override
	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
			
		HttpSession session = request.getSession();
		BlogPortal bp = new BlogPortalService().getPort(BlogPortal.class);
		
		AuthBlogPortalService us= new AuthBlogPortalService();
		W3CEndpointReference ref=(W3CEndpointReference)session.getAttribute("ref");
		System.out.println(ref);
		AuthBlogPortal userAuthenticate=us.getPort(ref, AuthBlogPortal.class);
		
		Integer id = Integer.parseInt(request.getParameter("id"));
		String object =  request.getParameter("object");
		session.removeAttribute("mess");
		
		if(object.equals("blog")){
			//Rimuovo il blog associato
			if(userAuthenticate.removeBlog(id)){
				
				User user = (User) session.getAttribute("user");
				Blog blog = bp.getBlogOfUser(user.getUserID());
				if(blog!=null && blog.getImgURL()!=null && !blog.getImgURL().equals("")){
					File a = new File("./webapps"+request.getContextPath()+"/blogs/"+blog.getImgURL());
					if(a.exists())
					   a.delete();
				}
				user.setBlogURL(null);
				session.setAttribute("user", user);
			}

			response.sendRedirect(response.encodeRedirectURL(request.getContextPath()+"/PortalServlet"));

			
		}else if(object.equals("post")){
			//REMOVE POST
			userAuthenticate.removePost(id);
			
			response.sendRedirect(response.encodeRedirectURL(request.getContextPath()+"/PortalServlet"));

			
		}else if(object.equals("comment")){
			//REMOVE COMMENT
			userAuthenticate.removeComment(id);
			int blogID=Integer.parseInt(request.getParameter("blogID"));
			int postID=Integer.parseInt(request.getParameter("postID"));
			
			response.sendRedirect(response.encodeRedirectURL(request.getContextPath()+"/PostView?blogID="+blogID+"&postID="+postID+"&p=1"));

		}else if(object.equals("user")){
			//REMOVE USER
			User user = (User) session.getAttribute("user");
			if(user.getImgURL()!=null && !user.getImgURL().equals("")){
				File a = new File("./webapps"+request.getContextPath()+"/users/"+user.getImgURL());
				if(a.exists())
				   a.delete();
			}
			Blog blog = bp.getBlogOfUser(user.getUserID());
			if(blog!=null && blog.getImgURL()!=null && !blog.getImgURL().equals("")){
				File b = new File("./webapps"+request.getContextPath()+"/blogs/"+blog.getImgURL());
				if(b.exists())
				b.delete();
			}
			userAuthenticate.removeUser(id);
			response.sendRedirect(response.encodeRedirectURL(request.getContextPath()+"/private/Logout"));


		}
		
	}
	


}
